package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CalendarPlan;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICalendarPlanService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("calendarPlan")
public class CalendarPlanController {
    @Autowired
    private ICalendarPlanService calendarPlanService;

    @RequiresPermissions(value = {"calendarPlan:view", "日程安排视图"}, logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "calendarPlan/calendarPlan";
    }

    @RequiresPermissions(value = {"calendarPlan:query", "日程安排列表"}, logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(QueryObject qo) {
        return calendarPlanService.query(qo);
    }

    @RequiresPermissions(value = {"calendarPlan:selectAll", "个人日程安排"}, logical = Logical.OR)
    @RequestMapping("selectAll")
    @ResponseBody
    public List<CalendarPlan> selectAll() {
        return calendarPlanService.selectAll();
    }

    @RequestMapping("selectById")
    @ResponseBody
    public CalendarPlan selectById(Long id) {
        return calendarPlanService.selectByPrimaryKey(id);
    }

    @RequiresPermissions(value = {"calendarPlan:delete", "日程安排删除"}, logical = Logical.OR)
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            calendarPlanService.deleteByPrimaryKey(id);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new JsonResult().mark("删除失败咯");
        }
        return new JsonResult();
    }

    @RequiresPermissions(value = {"calendarPlan:saveOrUpdate", "日程安排编辑/新增"}, logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(CalendarPlan entity) {
        try {
            if (entity.getId() == null) {
                calendarPlanService.insert(entity);
            } else {
                calendarPlanService.updateByPrimaryKey(entity);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }
}